package com.ft.springboottravel.controller;

import com.ft.springboottravel.bean.Category;
import com.ft.springboottravel.bean.Route;
import com.ft.springboottravel.bean.Shop;
import com.ft.springboottravel.service.CategoryService;
import com.ft.springboottravel.service.RouteService;
import com.ft.springboottravel.service.ShopService;
import com.ft.springboottravel.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/route")
public class RouteController {

    @Autowired
    private RouteService routeService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private CategoryService categoryService;


    @RequestMapping("/toRouteList")
    public String getRouteList(Model model,String titleName,Integer shop_id,Integer cate_id,Integer currentPage){


        if(titleName != null && titleName.length()>0){
            model.addAttribute("titleName",titleName);
        }
        if(shop_id != null){
            model.addAttribute("shop_id",shop_id);
        }
        if(cate_id != null){
            model.addAttribute("cate_id",cate_id);
        }

        Pager<Route> pager = routeService.selectRouteList(titleName, shop_id, cate_id, currentPage);

        model.addAttribute("pager",pager);

        return "route/routelist";
    }

    @RequestMapping("/add")
    public String add(Model model){

        List<Shop> shops = shopService.SelectShopList();
        List<Category> categories = categoryService.selectCategoryList();

        model.addAttribute("shops",shops);
        model.addAttribute("categories",categories);

        return "route/addroute";
    }

    @RequestMapping("/addRoute")
    public String addRoute(Route route,@RequestParam("routeImg") MultipartFile file){

        String name = file.getOriginalFilename();

        String lastPath = name.substring(name.lastIndexOf("."));

        String randomName = UUID.randomUUID().toString().replace("-", "")+lastPath;

        File file1 = new File("E:/uploads/route/" + randomName);

        System.out.println(randomName);

        try {
            if(!file1.exists()){
                file1.createNewFile();
            }
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        route.setCoverSrc("/route/" + randomName);
        routeService.addRoute(route);

        return "redirect:/route/toRouteList";
    }


    @RequestMapping("/edit")
    public String edit(Integer routeId,Model model, HttpSession session){
        Route route = routeService.selectRoute(routeId);

        List<Shop> shops = shopService.SelectShopList();
        List<Category> categories = categoryService.selectCategoryList();

        model.addAttribute("shops",shops);
        model.addAttribute("categories",categories);

        model.addAttribute("route",route);
        session.setAttribute("route",route);

        return "route/setroute";
    }

    @RequestMapping("/editRoute")
    public String editRoute(Route route,@RequestParam("routeImg") MultipartFile file,HttpSession session){

        Route route1 = (Route) session.getAttribute("route");


        String name = file.getOriginalFilename();

        if(file.getSize()>0){

            String lastPath = name.substring(name.lastIndexOf("."));

            String randomName = UUID.randomUUID().toString().replace("-", "")+lastPath;

            new File("E:/uploads" + route1.getCoverSrc()).delete();


            File file1 = new File("E:/uploads/route/" + randomName);

            try {
                file.transferTo(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            route.setCoverSrc("/route/" + randomName);
        }else{

            route.setCoverSrc(route1.getCoverSrc());

        }

        routeService.editRoute(route);

        return "redirect:/route/toRouteList";
    }


    @RequestMapping("/deleteRoute")
    public String deleteRoute(Integer routeId){

        routeService.deleteRoute(routeId);
        return "redirect:/route/toRouteList";
    }





}
